use strict;
use warnings;

use LWP::UserAgent;
use JSON;
use Data::Dumper;
use utf8;

my $gae_name = "airdata-feed.appspot.com";
my @ip_list = ("173.194.72.99","173.194.72.106");
my $location_url = 'http://%s/location/list';
my $airdata_url = 'http://%s/airdata/%s';

my $ua = LWP::UserAgent->new;
my $ip = $ip_list[0];
my $url = sprintf( $location_url, $gae_name);

$ua->proxy('http', 'http://127.0.0.1:8087/');
#$ua->default_header( "Host" => $gae_name );
#$ua->show_progress( 1 );
#$ua->timeout( 10 );

my $response = $ua->get( $url );

$response->is_success or
    die "Failed to GET '$url': ", $response->status_line;

my $json_text = $response->content;

my $json = decode_json( $json_text );

while(1) {
  for my $station (@$json) {
    my $id = ${$station}{"id"};
    print "$id =>";
    $url = sprintf( $airdata_url, $gae_name, $id);
    my $response = $ua->get( $url );
    if( $response->is_success ) {
      print $response->decoded_content, "\n";
    } else {
      print "fail\n";
    }
    sleep(1);
  }
}
